package com.gxa.modules.app.hospitalization.persional.service.impl;


import com.gxa.common.utils.IDCardUtil;
import com.gxa.common.utils.UUIDUtils;
import com.gxa.modules.app.hospitalization.persional.dto.PatientAddDate;
import com.gxa.modules.app.hospitalization.persional.dto.PatientUpdatedto;
import com.gxa.modules.app.hospitalization.persional.mapper.PatientMapper;
import com.gxa.modules.app.hospitalization.persional.service.PatientService;
import com.gxa.modules.model.entity.fee.Patient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

/**
 * @ClassName PatientServiceImpl
 * @Description
 * @Author Mr.YUAN
 * @Date 2022/11/12 11:27
 */
@Service
public class PatientServiceImpl implements PatientService {

    @Autowired
    private PatientMapper patientMapper;
    @Override
    public List<Patient> query(Integer id) {
        List<Patient> patients = patientMapper.selectAllById(id);

        return patients;
    }

    @Override
    public void add(PatientUpdatedto patientUpdatedto) {
        if (patientUpdatedto.getStatus() == 0) {
            patientMapper.updateStatus(patientUpdatedto.getUserId());
        }
        //就诊卡号
        String vistNum = "KH" + UUIDUtils.createOnlyId();
        //通过接受的参数，注入到自己的对象中进行添加
        int sex = IDCardUtil.getSex(patientUpdatedto.getCardNum());
        int age = IDCardUtil.getAge(patientUpdatedto.getCardNum());
        PatientAddDate patientAddDate = new PatientAddDate();
        patientAddDate.setAddress(patientUpdatedto.getAddress())
                        .setAge(age)
                        .setCardNum(patientUpdatedto.getCardNum())
                        .setIdcardType(patientUpdatedto.getIdcardType())
                        .setName(patientUpdatedto.getName())
                        .setPhone(patientUpdatedto.getPhone())
                        .setRelationType(patientUpdatedto.getRelationType())
                        .setSex(sex)
                        .setStatus(patientUpdatedto.getStatus())
                        .setUserId(patientUpdatedto.getUserId())
                        .setVisitNum(vistNum);
        patientMapper.addPatient(patientAddDate);

    }

    @Override
    public void deletebyId(Integer id) {
        patientMapper.deleteById(id);
    }

    @Override
    public void updatePatinet(PatientUpdatedto patientUpdatedto) {
        if (patientUpdatedto.getStatus() == 0) {
            patientMapper.updateStatus(patientUpdatedto.getUserId());
        }
        //通过接受的参数，注入到自己的对象中进行添加
        int sex = IDCardUtil.getSex(patientUpdatedto.getCardNum());
        int age = IDCardUtil.getAge(patientUpdatedto.getCardNum());
        PatientAddDate patientAddDate = new PatientAddDate();
        patientAddDate.setAddress(patientUpdatedto.getAddress())
                .setAge(age)
                .setId(patientUpdatedto.getId())
                .setCardNum(patientUpdatedto.getCardNum())
                .setIdcardType(patientUpdatedto.getIdcardType())
                .setName(patientUpdatedto.getName())
                .setPhone(patientUpdatedto.getPhone())
                .setRelationType(patientUpdatedto.getRelationType())
                .setSex(sex)
                .setStatus(patientUpdatedto.getStatus())
                .setUserId(patientUpdatedto.getUserId());
        patientMapper.update(patientAddDate);
        System.out.println("=状态====================》"+patientAddDate.getStatus());
    }
}